/*
	   _           _      _           _     _ 
	  (_)         | |    | |         (_)   | |
	   _ _   _ ___| |_ __| |_ __ ___  _  __| |
	  | | | | / __| __/ _` | '__/ _ \| |/ _` |
	  | | |_| \__ \ || (_| | | | (_) | | (_| |
	  | |\__,_|___/\__\__,_|_|  \___/|_|\__,_|
	 _/ |                                     
	|__/ 
	
	Copyright 2013 Caspar Isemer and and Eva Krueger, http://justdroid.org
	
	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at
	
		http://www.apache.org/licenses/LICENSE-2.0
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License. 
*/

package org.justdroid.justdroid;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

public class SplashActivity extends Activity {

	// Declare constants and variables
	private static String TAG = SplashActivity.class.getName();
	private static long SLEEP_TIME = 3600;
	
	/** Create the Splash screen on justdroid startup **/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Start call state changed service
		Intent bootIntent = new Intent(getApplicationContext(), CallStateService.class);
	    bootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	    startService(bootIntent);
		
		// Display splash screen without title and notification bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
	    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);	    
	    setContentView(R.layout.splash);
	    
	    // Vibrate for 1000 milliseconds
	    Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
	    vibrator.vibrate(700);
	 
	    // Start timer and launch main activity
	    IntentLauncher launcher = new IntentLauncher();
	    launcher.start();
	   }
	
	private class IntentLauncher extends Thread {
		@Override
		public void run() {
			try {
				// Sleeping
	            Thread.sleep(SLEEP_TIME);
	            
				// Increase the audio volume so that sounds can be heard
	            AudioManager audioManager = (AudioManager) getSystemService(SplashActivity.AUDIO_SERVICE); 
	            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
	            
				// Playback the justdroid.mp3 file
	            MediaPlayer mediaPlayer = MediaPlayer.create(getBaseContext(), R.raw.justdroid);
	            mediaPlayer.setLooping(false);
	            mediaPlayer.start();
	            
		        // Unload the justdroid.mp3 file once finished
		        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
		        	@Override
		        	public void onCompletion(MediaPlayer mediaPlayer) {
		        		// Unload the justdroid.mp3 file once finished
						mediaPlayer.stop();
						mediaPlayer.release();
						
						// Start main activity
						Intent intent = new Intent(SplashActivity.this, SwipingUtils.class);
						SplashActivity.this.startActivity(intent);
						SplashActivity.this.finish();
		        	}
		        });
	            
	         } catch (Exception e) {
	        	// Display the error in the LogCat window
	            Log.e(TAG, e.getMessage());
	            
	            // If justdroid.mp3 cannot be played, only then set the contentDecription of the justdroid logo
	            // ImageView so that TalkBack can read it out loud
	            ImageView imageView = (ImageView)findViewById(R.id.splash);
	            imageView.setContentDescription(getString(R.string.txtJustDroidActivated));
	         }
		}
	}
}
